package com.mashibing.disruptor.v1;

import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

public class LongEventProducer1 {

    private final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer1(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer buffer) throws InterruptedException {
        long sequence = ringBuffer.next();
        try {
            LongEvent event = ringBuffer.get(sequence);
            event.setValue(buffer.getLong(0));
            System.out.printf("\033[31m %s ===>> %s ===>> LongEventProducer1 ===>> %s \033[0m\n",
                    Thread.currentThread().getName(), LocalTime.now(), event.getValue());
        } finally {
            ringBuffer.publish(sequence);
        }
    }

}
